package com.pig4cloud.pig.task.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.HasPermission;
import com.pig4cloud.pig.task.entity.TaskEntity;
import com.pig4cloud.pig.task.service.TaskService;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.HttpHeaders;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务信息表
 *
 * @author pig
 * @date 2025-07-09 16:50:11
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/taskInfo" )
@Tag(description = "taskInfo" , name = "任务信息表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class TaskController {

    private final  TaskService taskService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param task 任务信息表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @HasPermission("task_taskInfo_view")
    public R getTaskPage(@ParameterObject Page page, @ParameterObject TaskEntity task) {
        LambdaQueryWrapper<TaskEntity> wrapper = Wrappers.lambdaQuery();

        return R.ok(taskService.page(page, wrapper));
    }



    /**
     * 通过条件查询任务信息表
     * @param task 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @HasPermission("task_taskInfo_view")
    public R getDetails(@SpringQueryMap TaskEntity task) {
        return R.ok(taskService.list(Wrappers.query(task)));
    }

    /**
     * 新增任务信息表
     * @param task 任务信息表
     * @return R
     */
    @Operation(summary = "新增任务信息表" , description = "新增任务信息表" )
    @SysLog("新增任务信息表" )
    @PostMapping
    @HasPermission("task_taskInfo_add")
    public R save(@RequestBody TaskEntity task) {
        return R.ok(taskService.save(task));
    }

    /**
     * 通过任务ID获取关联文件数据（返回字节数组）
     * @param taskId 任务ID
     * @return 文件字节数据
     */
    @Operation(summary = "通过任务ID获取文件数据", description = "返回文件的字节数组")
    @GetMapping("/file")
    @HasPermission("task_taskInfo_view")
    public R getFileDataByTaskId(@RequestParam Integer taskId) {

		return R.ok(taskService.getFileDataByTaskId(taskId));
    }

    /**
     * 修改任务信息表
     * @param task 任务信息表
     * @return R
     */
    @Operation(summary = "修改任务信息表" , description = "修改任务信息表" )
    @SysLog("修改任务信息表" )
    @PutMapping
    @HasPermission("task_taskInfo_edit")
    public R updateById(@RequestBody TaskEntity task) {
        return R.ok(taskService.updateById(task));
    }

    /**
     * 通过id删除任务信息表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除任务信息表" , description = "通过id删除任务信息表" )
    @SysLog("通过id删除任务信息表" )
    @DeleteMapping
    @HasPermission("task_taskInfo_del")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(taskService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param task 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @HasPermission("task_taskInfo_export")
    public List<TaskEntity> exportExcel(TaskEntity task,Long[] ids) {
        return taskService.list(Wrappers.lambdaQuery(task).in(ArrayUtil.isNotEmpty(ids), TaskEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param taskList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @HasPermission("task_taskInfo_export")
    public R importExcel(@RequestExcel List<TaskEntity> taskList, BindingResult bindingResult) {
        return R.ok(taskService.saveBatch(taskList));
    }
}
